package Form::Field::DateField;
use strict;
use warnings;
use base qw(My::Form::Field::Compound);
use Rose::HTML::Form::Field::PopUpMenu;

sub build_field {
    my ($self) = shift;
    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
    my $months = { '1'  => 'January',
                   '2'  => 'Febuary',
                   '3'  => 'March',
                   '4'  => 'April',
                   '5'  => 'May',
                   '6'  => 'June',
                   '7'  => 'July',
                   '8'  => 'August',
                   '9'  => 'September',
                   '10' => 'October',
                   '11' => 'November',
                   '12' => 'December',
    };
    my %params;
    $params{size}  = 1;
    $params{name}  = 'month';
    $params{class} = $self->class if ( $self->class );
    my $month_field = Rose::HTML::Form::Field::PopUpMenu->new( %params, required => $self->required );
    $params{name} = 'day';
    my $day_field = Rose::HTML::Form::Field::PopUpMenu->new( %params, required => $self->required );
    $params{name} = 'year';
    my $year_field =
        Rose::HTML::Form::Field::PopUpMenu->new( %params, required => $self->required );

    # initialise drop boxes
    $month_field->add_options( undef => undef );
    $day_field->add_options( undef => undef );
    $year_field->add_options( undef => undef );
    foreach my $month_id ( sort { $a <=> $b } ( keys( %{$months} ) ) ) {
        $month_field->add_options( $month_id => $months->{$month_id} );
    }
    for ( 1 .. 31 ) {
        $day_field->add_options( $_ => $_ );
    }
    for ( reverse( ( $year + 1900 - 100 ) .. ( $year + 1900 ) ) ) {
        $year_field->add_options( $_ => $_ );
    }
    $self->add_fields( day => $day_field, month => $month_field, year => $year_field, );
} ## end sub build_field

sub coalesce_value {
    my ($self) = shift;
    if ( $self->field_value('day') && $self->field_value('month') && $self->field_value('year') ) {
        return
              $self->field_value('day') . '/'
            . $self->field_value('month') . '/'
            . $self->field_value('year');
    }
}

sub decompose_value {
    my ( $self, $value ) = @_;
    return undef unless ( defined($value) );
    if ( $value =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) {
        return { day => $1, month => $2, year => $3, };
    }
} ## end sub decompose_value

sub html {
    my ($self) = shift;
    my $html =
        $self->field('day')->html . '/' . $self->field('month')->html . '/' . $self->field('year')->html;
    return $html . $self->html_error();
} ## end sub html

sub validate {
    my ($self) = shift;
    if ( !$self->SUPER::validate() ) { return; }
    if (    $self->required
         && $self->field('day')->internal_value
         && $self->field('month')->internal_value
         && $self->field('year')->internal_value )
    {
        return 1;
    } elsif ( !$self->required ) {
        return 1;
    }
}
1;
